﻿using CQRSFramework.Common.IO;
using CQRSFramework.Events;
using CQRSFramework.Test.events;
using System;
using System.Threading.Tasks;

namespace CQRSFramework.Test.eventHandlers
{
    public class AccountEventHandler : IEventHandler<AccountCreatedEvent>
        , IEventHandler<AccountNameChangedEvent>
    {
        public Task<AsyncTaskResult> HandleAsync(AccountCreatedEvent evnt)
        {
            Console.WriteLine("account created:" + evnt.AggregateRootId);
            //using (var conn = new MySqlConnection("Server =10.0.1.195 ; Port = 3306; Database = eventstore; Uid = root; Pwd = 123456; charset = utf8; SslMode = None"))
            //{
            //    conn.Execute($"insert into account(username,userid,version) values ('{evnt.Name}','{evnt.UserId}','{evnt.Version}')");
            //}
            return Task.FromResult(AsyncTaskResult.Success);
        }

        public Task<AsyncTaskResult> HandleAsync(AccountNameChangedEvent evnt)
        {
            Console.WriteLine("account name changed to :" + evnt.Name + ",version:" + evnt.Version + ",aggrategrateId:" + evnt.AggregateRootId);
            //using (var conn = new MySqlConnection("Server =10.0.1.195 ; Port = 3306; Database = eventstore; Uid = root; Pwd = 123456; charset = utf8; SslMode = None"))
            //{
            //    conn.Execute($"update account set username='{evnt.Name}',version='{evnt.Version}' where userid='{evnt.UserId}' and version='{evnt.Version - 1}' ");
            //}
            return Task.FromResult(AsyncTaskResult.Success);
        }
    }
}
